﻿Public Class clsOldDataContainer

    Private m_sTuvDate As Date
    Private m_dMileage As Single
    Private m_sUpdatePath As String
    Private m_pOption1 As List(Of Integer)
    Private m_pOption2 As List(Of Integer)
    Private m_pOption3 As List(Of String)

    Private m_pRefuelingList As List(Of clsRefueling)
    Private m_pMiscList As List(Of clsMisc)

    'Private Const FILENAME As String = "Data.kad"

    Private Sub New()
        m_pOption1 = New List(Of Integer)
        m_pOption2 = New List(Of Integer)
        m_pOption3 = New List(Of String)
        m_pRefuelingList = New List(Of clsRefueling)
        m_pMiscList = New List(Of clsMisc)
    End Sub

    Public Property TuvDate As Date
        Get
            Return m_sTuvDate
        End Get
        Private Set(ByVal value As Date)
            m_sTuvDate = value
        End Set
    End Property
    Public Property Mileage As Single
        Get
            Return m_dMileage
        End Get
        Private Set(ByVal value As Single)
            m_dMileage = value
        End Set
    End Property
    Public Property UpdatePath As String
        Get
            Return m_sUpdatePath
        End Get
        Private Set(ByVal value As String)
            m_sUpdatePath = value
        End Set
    End Property
    Public Property Option1 As List(Of Integer)
        Get
            Return m_pOption1
        End Get
        Private Set(ByVal value As List(Of Integer))
            m_pOption1 = value
        End Set
    End Property
    Public Property Option2 As List(Of Integer)
        Get
            Return m_pOption2
        End Get
        Private Set(ByVal value As List(Of Integer))
            m_pOption2 = value
        End Set
    End Property
    Public Property Option3 As List(Of String)
        Get
            Return m_pOption3
        End Get
        Private Set(ByVal value As List(Of String))
            m_pOption3 = value
        End Set
    End Property
    Public Property RefuelingList As List(Of clsRefueling)
        Get
            Return m_pRefuelingList
        End Get
        Private Set(ByVal value As List(Of clsRefueling))
            m_pRefuelingList = value
        End Set
    End Property
    Public Property MiscList As List(Of clsMisc)
        Get
            Return m_pMiscList
        End Get
        Private Set(ByVal value As List(Of clsMisc))
            m_pMiscList = value
        End Set
    End Property

    Public Shared Function LoadOldData(ByVal sPath As String) As clsOldDataContainer
        Try
            If Not IO.File.Exists(sPath) Then Return Nothing

            Dim pOldData As New clsOldDataContainer

            Dim sLines As List(Of String) = ReadOldFile(sPath)

            With pOldData

                Dim dLastDistance As Single = 0.0

                For Each sLine In sLines
                    sLine = sLine.Trim

                    If sLine.ToLower.StartsWith("tüv") Then
                        sLine = sLine.Replace("tüv", String.Empty)
                        .TuvDate = Date.Parse(sLine)

                    ElseIf sLine.ToLower.StartsWith("kmstand") Then
                        sLine = sLine.Replace("kmstand", String.Empty)
                        .Mileage = Single.Parse(sLine)

                    ElseIf sLine.ToLower.StartsWith("update") Then
                        sLine = sLine.Replace("update", String.Empty)
                        .UpdatePath = sLine

                    ElseIf sLine.ToLower.StartsWith("opt01") Then
                        sLine = sLine.Replace("opt01", String.Empty)
                        Dim sOpt As String() = sLine.Split("|")
                        For Each sOption In sOpt
                            If Not (sOption = String.Empty) Then
                                .Option1.Add(sOption)
                            End If
                        Next

                    ElseIf sLine.ToLower.StartsWith("opt02") Then
                        sLine = sLine.Replace("opt02", String.Empty)
                        Dim sOpts As String() = sLine.Split("|")
                        For Each sOption In sOpts
                            If Not (sOption = String.Empty) Then
                                .Option2.Add(sOption)
                            End If
                        Next

                    ElseIf sLine.ToLower.StartsWith("opt03") Then
                        sLine = sLine.Replace("opt03", String.Empty)
                        Dim sOpts As String() = sLine.Split("|")
                        For Each sOption In sOpts
                            If Not (sOption = String.Empty) Then
                                .Option3.Add(sOption)
                            End If
                        Next

                    ElseIf sLine.ToLower.StartsWith("tank") Then
                        Try
                            sLine = sLine.Replace("tank", String.Empty)
                            Dim sPieces As String() = sLine.Split("|")
                            Dim pRefueling As New clsRefueling
                            Date.TryParse(sPieces(0), pRefueling.RefuelDate)
                            Single.TryParse(sPieces(1), pRefueling.Price)
                            Single.TryParse(sPieces(2), pRefueling.Liter)
                            Single.TryParse(sPieces(3), pRefueling.PricePerLiter)
                            pRefueling.GasStation = sPieces(4)
                            pRefueling.LastDistance = dLastDistance
                            Single.TryParse(sPieces(5), dLastDistance)

                            .RefuelingList.Add(pRefueling)
                        Catch ex As Exception
                            clsLogWriter.WriteLog(LogLevel.WARNING, "Ein alter Eintrag eines Tankvorganges konnte nicht interpretiert werden", ex)
                        End Try


                    ElseIf sLine.ToLower.StartsWith("div") Then
                        Try
                            sLine = sLine.Replace("div", String.Empty)
                            Dim sPieces As String() = sLine.Split("|")
                            Dim pMisc As New clsMisc

                            pMisc.MiscDate = Date.Parse(sPieces(0))
                            pMisc.Description = sPieces(1)
                            pMisc.Cost = sPieces(2)
                            pMisc.Note = sPieces(3)

                            .MiscList.Add(pMisc)
                        Catch ex As Exception
                            clsLogWriter.WriteLog(LogLevel.WARNING, "Ein alter Eintrag sonstiger Kosten konnte nicht interpretiert werden", ex)
                        End Try

                    End If
                Next

            End With


            ' Datensätze intern nach Datum sortieren
            clsFunctions.SortRefueling(pOldData.m_pRefuelingList)

            Return pOldData

        Catch ex As Exception
            clsLogWriter.WriteLog(LogLevel.ERROR, "Beim Laden alter Daten ist ein Fehler aufgetreten", ex)
        End Try

        Return Nothing
    End Function



    Public Sub AssignToNewSettings()
        clsSettings.Setting("nextInspection") = Me.TuvDate.ToString("MM.yyyy")
        clsSettings.Setting("mileage") = Me.Mileage

        clsSettings.Setting("chFuelDateWidth") = Me.Option1(0)
        clsSettings.Setting("chFuelPriceWidth") = Me.Option1(1)
        clsSettings.Setting("chFuelLiterWidth") = Me.Option1(2)
        clsSettings.Setting("chFuelPricePerLiterWidth") = Me.Option1(3)
        clsSettings.Setting("chFuelGasStationWidth") = Me.Option1(4)
        clsSettings.Setting("chFuelKilometerWidth") = Me.Option1(5)

        clsSettings.Setting("chMiscDateWidth") = Me.Option2(0)
        clsSettings.Setting("chMiscDescriptionWidth") = Me.Option2(1)
        clsSettings.Setting("chMiscCostWidth") = Me.Option2(2)
        clsSettings.Setting("chMiscNoteWidth") = Me.Option2(3)
    End Sub

    Private Shared Function ReadOldFile(ByVal sFilePath As String) As List(Of String)
        Dim sLines As New List(Of String)
        Dim pStreamReader As IO.StreamReader = Nothing

        Try
            pStreamReader = New IO.StreamReader(sFilePath)

            Do Until pStreamReader.EndOfStream
                sLines.Add(pStreamReader.ReadLine)
            Loop

        Catch ex As Exception
            clsLogWriter.WriteLog(LogLevel.ERROR, "Fehler beim Lesen alter Daten", ex)

        Finally
            If pStreamReader IsNot Nothing Then
                pStreamReader.Close()
                pStreamReader.Dispose()
            End If

        End Try

        Return sLines
    End Function

End Class
